package com.qianfeng.smartmetting.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianfeng.smartmetting.entity.User;
import com.qianfeng.smartmetting.service.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsPasswordService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;


@Component
public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails updatePassword(UserDetails userDetails, String s) {
        return null;
    }

    @Override
    public void createUser(UserDetails userDetails) {
        System.out.println("username:"+userDetails.getUsername()+",password:"+userDetails.getPassword());
    }

    @Override
    public void updateUser(UserDetails userDetails) {

    }



    @Override
    public void deleteUser(String s) {

    }

    @Override
    public void changePassword(String s, String s1) {

    }

    @Override
    public boolean userExists(String s) {
        return false;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //参数s 即前端传入的用户名/登录名
        //通过用户名查询用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",s);
        User user = userDao.getOne(queryWrapper);

        if(user == null){
            //当用户不存在，抛出异常
            throw new UsernameNotFoundException("用户不存在");
        }else{
            Collection<GrantedAuthority> authorities = new ArrayList<>();
            //加入多个角色，需要进行多次添加
            authorities.add(new SimpleGrantedAuthority(user.getUserRole()));
            return new org.springframework.security.core.userdetails.User(
                    user.getUserName(), //用户名
                    user.getUserPassword(), //密码
                    user.getUserEnable() == 1?true:false, //是否启用
                    true, //账户是否过期
                    true, //证书是否过期
                    true, //账户是否锁定
                    authorities //权限集合
            );
        }

    }
}
